<?php

class Default_GuestbookAction extends ProjectBaseAction
{
	 public function executeRead(AgaviRequestDataHolder $rd)
	 {
	 	return 'Success';  	
	 }

	 public function executeWrite(AgaviRequestDataHolder $rd)
	 {
	 	$afg = new AfgOMComment();
        $afg->setName($rd->getParameter('name'));
		$afg->setEmail($rd->getParameter('email'));
		$afg->setUrl($rd->getParameter('homepage'));
		$afg->setMessage($rd->getParameter('message'));
		$afg->setCreateTs(time());
		$afg->setIp($_SERVER['REMOTE_ADDR']);
		
		$msgKey = md5($rd->getParameter('message').$rd->getParameter('name').$rd->getParameter('email').$rd->getParameter('homepage'));
		if($msgKey != $this->getContext()->getUser()->getAttribute('lastEntryKey')) {
		    $afg->save();
	        $this->getContext()->getUser()->setAttribute('lastEntryKey',$msgKey);
	        $this->setAttribute('saveEntry',true);
		} else {
			$this->setAttribute('doubleEntry',true);
		}
		
        $this->setAttribute('resetForm',true);
                
		return 'Success'; 
	 }
	 
	public function getCredentials() {
		// something the user doesn't have ;)
		return array('zomglolkthxbai');
	}

	public function handleError(AgaviRequestDataHolder $rd) {
		return "Success";
	}
	
	public function getDefaultViewName() {
		
		return 'Success';
	}
	
	public function validateWrite(AgaviRequestDataHolder $rd)
	{
		$c = new Criteria();
		$c->add(AfgOMCommentPeer::CREATE_TS,(time()-5),Criteria::GREATER_EQUAL);
		$c->add(AfgOMCommentPeer::IP, $_SERVER['REMOTE_ADDR'], Criteria::EQUAL);
		$c->setLimit(1);
		$comment = AfgOMCommentPeer::doSelectOne($c);
		
		if ($comment instanceof AfgOMComment) {
			$vm = $this->getContainer()->getValidationManager();
			$vm->setError('nomessages','Spamer!!!');
		  
		  	return false;
		}
	  	return true;
	}	
}

?>